package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dto.TransMarketCodeDto;
import com.tianqu.tms.entity.TransMarketCode;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TransMarketCodeInfoAjaxAction  extends TmsAjaxAction{

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {


		logger.debug(this.getClass().getName() + "#execute.");
		String query = request.getParameter("query");

		TransMarketCode transMarketCode = TransMarketCode.getInstance();
		List<TransMarketCodeDto> list = transMarketCode.getTransMarketCodeInfoByPy(query);

		List r = new ArrayList();
		for (TransMarketCodeDto transMarketCodeDto : list) {
			HashMap map = new HashMap();
			map.put("value", transMarketCodeDto.getMarketName());
			map.put("data", transMarketCodeDto.getId());
			r.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(JSONArray.fromObject(r));
		JSONObject json = new JSONObject();
		json.put("query", query);
		json.put("suggestions", jsonArray);
		responseValues(response, json.toString(), TEXT);

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	
	}

}
